package com.unlcn.ils.kas.service.sysadmin.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.unlcn.ils.kas.dao.mapper.sysadmin.OrgMapper;
import com.unlcn.ils.kas.dao.model.sysadmin.Org;
import com.unlcn.ils.kas.service.sysadmin.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoguixin on 2017/12/10.
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public int addOrg(Org org) {
        Assert.notNull(org, "组织不能为空");
        EntityWrapper<Org> wrapper = new EntityWrapper<Org>();
        wrapper.setEntity(org);
        if (orgMapper.selectCount(wrapper) <= 0) {
            return orgMapper.insert(org);
        } else {
            return 0;
        }
    }

    @Override
    public int updateOrg(Org org) {
        return orgMapper.updateById(org);
    }

    @Override
    public int deleteOrg(List<Integer> orgIds) {
        return orgMapper.deleteBatchIds(orgIds);
    }

    @Override
    public List<Org> listAllOrg() {
        return orgMapper.selectList(null);
    }

    @Override
    public List<Org> listOrgByPid(Integer parentId) {
        Map<String, Object> map = new HashMap<>();
        map.put("parent", parentId);
        return orgMapper.selectByMap(map);
    }
}
